package middleware

import (
	"github.com/dxvgef/sessions"
	"github.com/gin-gonic/gin"
	"log"
	"mytest/util"
)

func SessionMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		session, err := util.SessionStart(c.Request, c.Writer)
		if err != nil {
			log.Println(err.Error())
			return
		}
		var result sessions.Result
		result = session.Get("userinfo")
		if result.Err() != nil {
			c.Status(401)
			c.Abort()
			return
		}

		c.Next()
	}
}
